package com.vvlive.caibao.smartcity.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.kymjs.kjframe.KJBitmap;

import java.util.List;

/**
 * Created by zhj on 2015/9/2.
 * 简化RecyclerView的adapter操作
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter {

    private List<T> list;
    private int mItemId;
    private LayoutInflater mInflater;
    private int mWidth;
    private LinearLayout.LayoutParams mParams;
    private AdapterClickListener mAdapterClickListener;
    private OnItemClickListener onItemClickListener;

    public abstract void onBindData(ViewHolder viewHolder, T data, int position);

    public BaseRecyclerAdapter(Context context, List<T> list, int itemId) {
        this.list = list;
        this.mItemId = itemId;
        mInflater = LayoutInflater.from(context);
        mWidth = context.getResources().getDisplayMetrics().widthPixels;
        mParams = new LinearLayout.LayoutParams(mWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        mAdapterClickListener = new AdapterClickListener();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(mItemId, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.itemView.setLayoutParams(mParams);
        viewHolder.itemView.setOnClickListener(mAdapterClickListener);
        viewHolder.itemView.setTag(position);
        T bindData = list.get(position);
        onBindData(viewHolder, bindData, position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public List<T> getData() {
        return list;
    }

    public interface OnItemClickListener{
        void onItemClick(RecyclerView.Adapter adapter, int position);
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * ViewHolder
     */
    public final class ViewHolder extends RecyclerView.ViewHolder {

        private KJBitmap mKJBitmap;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        /**
         * 给TextView设置文字
         * @param id
         * @param text
         */
        public void setText(int id, String text) {
            TextView textView = findView(id);
            textView.setText(text);
        }

        /**
         * 给ImageView设置来自资源的图片
         * @param id
         * @param res
         */
        public void setImageResource(int id, int res) {
            ImageView imageView = findView(id);
            imageView.setImageResource(res);
        }

        /**
         * 给ImageView设置网络图片
         * @param id
         * @param url
         */
        public void setImageUrl(int id, String url) {
            if (mKJBitmap == null) {
                mKJBitmap = new KJBitmap();
            }
            ImageView imageView = findView(id);
            mKJBitmap.display(imageView, url);
        }

        /**
         * 查找控件
         * @param id
         * @param <T>
         * @return
         */
        public <T extends View> T findView(int id) {
            return (T) super.itemView.findViewById(id);
        }
    }

    /**
     *
     */
    private final class AdapterClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            if(onItemClickListener != null){
                onItemClickListener.onItemClick(BaseRecyclerAdapter.this, position);
            }
        }
    }

//    class Test extends BaseRecyclerAdapter<String>{
//
//        public Test(Context context, List<String> list, int itemId) {
//            super(context, list, itemId);
//        }
//
//        @Override
//        public void onBindData(ViewHolder viewHolder, String data, int position) {
//
//        }
//    }
}
